package com.nineclock.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.nineclock.system.service.SysOrganizationService;
import com.nineclock.system.service.impl.SysOrganizationServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
@Slf4j
public class ExcelListener extends AnalysisEventListener<ExcelMember> {
    List<ExcelMember> excelMembers = new ArrayList<>();
    private SysOrganizationService sysOrganizationService;

    public ExcelListener() {
        sysOrganizationService = new SysOrganizationServiceImpl();
    }

    public ExcelListener(SysOrganizationService sysOrganizationService) {
        this.sysOrganizationService = sysOrganizationService;
    }

    @Override
    public void invoke(ExcelMember data, AnalysisContext context) {
        log.info("解析到的数据:" + data);
        if (data != null) {
            excelMembers.add(data);
        }
        log.info("解析完毕");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        sysOrganizationService.saveExcelUser(excelMembers);
        log.info("持久化到数据库完成");
    }
}
